package Yangtz.Cs.Utils;

import java.sql.*;

public class JDBCUtil {
    Connection conn=null;
    public   Connection GetConnection() throws ClassNotFoundException {
        //驱动路径
        String driver="com.mysql.cj.jdbc.Driver";
        //数据库地址
        String url="jdbc:mysql://localhost/webbkdb?serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull";
        //用户名
        String username="root";
        //密码
        String password="lxr011221";
        Class.forName(driver);
        try{
            conn= DriverManager.getConnection(url,username,password);
        }catch (Exception e){
            e.printStackTrace();
        }
        return  conn;
    }

    // 关闭数据库连接，释放资源
    public static void release(Statement stmt, Connection conn) {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            stmt = null;
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            conn = null;
        }
    }
    public static void release(ResultSet rs, Statement stmt,
                               Connection conn){
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            rs = null;
        }
        release(stmt, conn);
    }
}
